import { createApp } from 'vue'
import App from './App.vue'

// 引入路由
import Router from '@/routers/index';

// 引入Vant UI
import Vant from 'vant';
// 引入Vant UI组件样式
import 'vant/lib/index.css';
// 引入封装请求
import Request from '@/services/request';
// 引入工具函数
import Utils from '@/utils/utils';
// 引入pinia
import { createPinia } from 'pinia';
// 引入pinia持久化插件
import piniaPluginPersistedstate from 'pinia-plugin-persistedstate';
// 引入Vue3Cookie
import Cookies from 'vue3-cookies';

const app = createApp(App)
.use(Router)
.use(Vant)
.use(Request)
.use(createPinia().use(piniaPluginPersistedstate))
.use(Utils)
.use(Cookies)
.mount('#app');

// 路由守卫
Router.beforeEach(async (to,from) => {
    // 获取当前跳转页面是否需要登录才访问
    let IsLogin = to.meta?.IsLogin === true ? true : false;

    if(IsLogin === false)
    {
        return true;
    }

    if(Object.getOwnPropertyNames(app.$business.value).length === 0)
    {
        app.$fail('请先登录');

        return {path:'/business/login'};
    }

    let id = app.$business.value.id ?? 0;
    // let mobile = app.$business.value.mobile ?? '';

    let result = await app.$http.post({
        url:'/business/check',
        params:{
            id,
            // mobile
        }
    })

    if(result.code === 1)
    {
        app.$business.value = result.data;

        return true;
    }else{
        app.$business.value = {};

        app.$fail(result.msg);

        return {path:'/business/login'};
    }
})